package admin

import (
	"context"

	"suci-go-zero/common/xerr"
	"suci-go-zero/internal/model"
	"suci-go-zero/internal/svc"
	"suci-go-zero/internal/types"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type RoleUpLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleUpLogic {
	return &RoleUpLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleUpLogic) RoleUp(req *types.Role) error {
	role, err := l.svcCtx.Models.Role.FindOne(l.ctx, int64(req.Id))
	if err != nil && err != model.ErrNotFound {
		return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "role 查找错误%v", role)
	}

	role.Alias = req.Alias
	role.Label = req.Label
	role.Remark = req.Remark
	role.Sort = int64(req.Sort)
	role.Status = int64(req.Status)

	err = l.svcCtx.Models.Role.UpdateWithVersion(l.ctx, nil, role)
	if err != nil {
		return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "role 更新数据错误%v", err)
	}

	return nil
}
